import { ElMessage } from "element-plus";
import router from "./router";
import useUserStore from "./store/modules/user";

const whiteList = ['/login', '/register', '/error']

router.beforeEach(async (to, from, next) => {
  const useUser = useUserStore();
  const token = useUser.token;

  if (to.path === '/error') {
    return next();
  }

  if (token) {
    if (useUser.status === 'disable') {
      return next('/error');
    }
    if (to.path === '/login' || to.path === '/register') {
      return next('/');
    }
    if(to.path === '/user/manage' && useUser.role !== 'admin'){
      return next('/')
    }
    return next();
  } else {
    if (whiteList.includes(to.path)) {
      return next();
    }
    ElMessage.error('请先登录');
    return next('/login');
  }
});

router.afterEach(() => {
})